/*
 * Copyright (c) 2013 - 2015, Freescale Semiconductor, Inc.
 * Copyright 2016-2017 NXP
 * All rights reserved.
 *
 * SPDX-License-Identifier: BSD-3-Clause
 */

#include "board.h"
#include "clock_config.h"
#include "fsl_debug_console.h"
#include "fsl_device_registers.h"
#include "pin_mux.h"
#include "fsl_lpuart.h"
#include "fsl_lpuart_edma.h"
#include "fsl_dmamux.h"

// JLinkGDBServer -select USB -device MIMXRT1061xxx5A -endian little -if SWD
// -speed 4000 -noir -LocalhostOnly
/*******************************************************************************
 * Definitions
 ******************************************************************************/

/*******************************************************************************
 * Prototypes
 ******************************************************************************/

/*******************************************************************************
 * Code
 ******************************************************************************/
/*!
 * @brief Main function
 */

void setup_poll_test(void) {
  lpuart_config_t cfg;

  LPUART_GetDefaultConfig(&cfg);
  cfg.baudRate_Bps = 115200;
  cfg.enableTx = true;
  cfg.enableRx = true;

  LPUART_Init(LPUART1, &cfg, BOARD_DebugConsoleSrcFreq());

  LPUART_WriteBlocking(LPUART1, (uint8_t *)"hello world,me", 13);
}

void poll_test_main(void) {
  uint8_t buf[32];

  while (1) {
    LPUART_ReadBlocking(LPUART1, buf, 5);
    LPUART_WriteBlocking(LPUART1, buf, 5);
  }
}

void setup_irq_test(void) {
  lpuart_config_t cfg;

  LPUART_GetDefaultConfig(&cfg);
  cfg.baudRate_Bps = 115200;
  cfg.enableTx = true;
  cfg.enableRx = true;

  LPUART_Init(LPUART1, &cfg, BOARD_DebugConsoleSrcFreq());

  LPUART_EnableInterrupts(LPUART1,kLPUART_RxDataRegFullFlag);
  EnableIRQ(LPUART1_IRQn);
  LPUART_WriteBlocking(LPUART1, (uint8_t*)"hello world,me", 13);
}

void LPUART1_IRQHandler(){
    uint8_t ch;
    if(LPUART_GetStatusFlags(LPUART1)&& kLPUART_RxDataRegFullFlag){
        ch = LPUART_ReadByte(LPUART1);
        LPUART_WriteByte(LPUART1,ch);
    }

    SDK_ISR_EXIT_BARRIER;
}


#define UART_TX_DMA_CHANNEL  (0U)
#define UART_RX_DMA_CHANNEL  (1U)

edma_handle_t tx_edma_handle;
edma_handle_t rx_edma_handle;
lpuart_edma_handle_t uart_dma_handle;

void uart_call_back(LPUART_Type *base, lpuart_edma_handle_t *handle, status_t status, void *userData)
{
    userData = userData;
}

void setup_dma_test(void){
  lpuart_config_t cfg;

  LPUART_GetDefaultConfig(&cfg);
  cfg.baudRate_Bps = 115200;
  cfg.enableTx = true;
  cfg.enableRx = true;

  LPUART_Init(LPUART1, &cfg, BOARD_DebugConsoleSrcFreq());

  // setup dmamux 
  DMAMUX_Init(DMAMUX);
  DMAMUX_SetSource(DMAMUX,0U,kDmaRequestMuxLPUART1Tx);
  DMAMUX_SetSource(DMAMUX,1U,kDmaRequestMuxLPUART1Rx);

  DMAMUX_EnableChannel(DMAMUX,UART_TX_DMA_CHANNEL);
  DMAMUX_EnableChannel(DMAMUX,UART_RX_DMA_CHANNEL);

  // setup dma channel
  edma_config_t dma_cfg;
  EDMA_GetDefaultConfig(&dma_cfg);
  EDMA_CreateHandle(&tx_edma_handle,DMA0,UART_TX_DMA_CHANNEL);
  EDMA_CreateHandle(&rx_edma_handle,DMA0,UART_RX_DMA_CHANNEL);

  
  LPUART_TransferCreateHandleEDMA(LPUART1, &uart_dma_handle, uart_call_back, NULL, &tx_edma_handle,
                                    &rx_edma_handle);
  lpuart_transfer_t xfer;
    /* Send g_tipString out. */
    xfer.data     = "hello, dma";
    xfer.dataSize = strlen(xfer.data);
    LPUART_SendEDMA(LPUART1, &uart_dma_handle, &xfer);

}

void dma_test_main(){

}

int main(void) {
  /* Init board hardware. */
  BOARD_ConfigMPU();
  BOARD_InitBootPins();
  BOARD_InitBootClocks();
  // BOARD_InitDebugConsole();

  setup_dma_test();
  dma_test_main();
  while (1) {
  };
}
